package com.woniu.cms.contract.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.cms.contract.core.entity.AreaVo;
import com.woniu.cms.contract.core.entity.Bid;
import com.woniu.cms.contract.core.entity.FrameworkContract;
import com.woniu.cms.contract.core.mapper.FrameworkContractMapper;
import com.woniu.cms.contract.core.service.IBidService;
import com.woniu.cms.contract.core.service.IFrameworkContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@Service
public class FrameworkContractServiceImpl extends ServiceImpl<FrameworkContractMapper, FrameworkContract> implements IFrameworkContractService {

    @Autowired
    private FrameworkContractMapper frameworkContractMapper;

    @Autowired
    private IBidService bidService;

    /*
    * 1.判断插入数据，添加框架合同 自动生成编号
    * 2.判断插入数据，审批
    * */
    @Override
    public Result addAuditFramework(FrameworkContract frameworkContract) {



        if (StringUtils.isEmpty(frameworkContract.getApprovalResult())){
            frameworkContract.setApprovalResult(2);

            frameworkContract.setSubmitTime(new Date());

            //设置时间格式，截取时间年份
            SimpleDateFormat format = new SimpleDateFormat("yyyy");
            //将时间转成String
            String date = format.format(new Date());
            //根据用户输入地区，查询地区的编号
            String region = frameworkContract.getRegion();
            AreaVo areaVo = bidService.areaName(region);

            //截取编号后四位
            String substring = areaVo.getAreaId().substring(2, 6);

            //使用年份+编号进行模糊查询，得到list集合
            List<FrameworkContract> frameworkContracts = this.generatedNumber("FC", date, substring);

            //判断集合是否有数据
            if (frameworkContracts.size() == 0){
                //数据库没有数据则自添一条
                frameworkContract.setFrameworkContractCode("FC" + date + substring + "001");
                System.out.println(frameworkContract.getFrameworkContractCode());
            }else{
                //拿到集合的第一条数据
                FrameworkContract contract = frameworkContracts.get(0);
                System.out.println("contract = " + contract);

                //拿到数据中最新的投标编号
                String fcCode = contract.getFrameworkContractCode();
                System.out.println("code = " + fcCode);
                //截取字段进行增加
                String substring1 = fcCode.substring(6, fcCode.length());
                Long i = Long.valueOf(Integer.parseInt(substring1));
                ++i;
                System.out.println("i = " + i);
                String s = String.valueOf(i);
                //截取字段，进行合并，添加到对象中
                String substring2 = fcCode.substring(0, 7);
                String s2 = substring2 + s;
                System.out.println("s2 = " + s2);
                frameworkContract.setFrameworkContractCode(s2);


            }
            boolean b = this.save(frameworkContract);

            return b == true ? Result.ok() : Result.error();
        }

        if (frameworkContract.getApprovalResult() == 2){
            return Result.error("审核中的数据不能进行修改");
        }

        if ((frameworkContract.getApprovalResult() == 0 || frameworkContract.getApprovalResult() == 1) && StringUtils.isEmpty(frameworkContract.getApprovalTime())){
            frameworkContract.setApprovalTime(new Date());
        }

        if (frameworkContract.getApprovalResult() == 0){
            frameworkContract.setApprovalResult(2);
        }

        if (frameworkContract.getFrameworkContractCode() == null || frameworkContract.getFrameworkContractCode() == ""){
            return Result.error("编号不能为空");
        }

        QueryWrapper<FrameworkContract> wrapper = new QueryWrapper<>();
        wrapper.eq("framework_contract_code",frameworkContract.getFrameworkContractCode());
        boolean b = this.update(frameworkContract, wrapper);
        return b == true ? Result.ok() : Result.error();
    }

    /*
    *
    * 查询单个合同，用于审批编辑显示
    * */
    @Override
    public Result queryFramework(String frameworkContractCode) {

        QueryWrapper<FrameworkContract> wrapper = new QueryWrapper<>();
        wrapper.eq("framework_contract_code",frameworkContractCode);

        FrameworkContract frameworkContract = this.getOne(wrapper);

        return Result.common(frameworkContract);
    }

    /*
    * 用于生成编号的查询
    * */
    @Override
    public List<FrameworkContract> generatedNumber(String fc, String date, String area) {

        List<FrameworkContract> frameworkContracts = frameworkContractMapper.generatedNumber(fc, date, area);

        return frameworkContracts;
    }

    /*
    * 根据合同编号删除
    * */
    @Override
    public Result deleFramework(String frameworkContractCode) {
        QueryWrapper<FrameworkContract> wrapper = new QueryWrapper<>();
        wrapper.eq("framework_contract_code",frameworkContractCode);
        wrapper.eq("approval_result",0);
        boolean b = this.remove(wrapper);
        return b == true ? Result.ok() : Result.error();
    }













 /*   public Result auditFramework(FrameworkContract frameworkContract){

        boolean b = this.save(frameworkContract);

    }*/


}
